﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using CommonClass;
using System.Diagnostics;
using SurveillanceLib;


namespace ControlLib
{
    /// <summary>
    /// 预览界面
    /// </summary>
    public partial class WallPreview : UserControl
    {
        #region 属性

        /// <summary>
        /// 基窗口，用于动态分屏
        /// </summary>
        public Panel TablePanel
        {
            get
            {
                return panelTable;
            }
        }

        /// <summary>
        /// 记录当前所有分屏
        /// </summary>
        public List<Panel> PanelCurrentList { get; set; }

        /// <summary>
        /// 大屏序号
        /// </summary>
        public string WallNo
        {
            get
            {
                return lbWallNo.Text;
            }
            set
            {
                if (lbWallNo.Text.Equals(value)) return;
                lbWallNo.Text = value;
            }
        }

        /// <summary>
        /// 已设置
        /// </summary>
        public string OKSum
        {
            get
            {
                return lbOK.Text;
            }
            set
            {
                if (lbOK.Text.Equals(value)) return;
                lbOK.Text = value;
            }
        }

        /// <summary>
        /// 未设置
        /// </summary>
        public string FaultySum
        {
            get
            {
                return lbFaulty.Text;
            }
            set
            {
                if (lbFaulty.Text.Equals(value)) return;
                lbFaulty.Text = value;
            }
        }

        public bool? _isShow;
        /// <summary>
        /// 是否在上屏上显示
        /// </summary>
        public bool? IsShowWall
        {
            get
            {
                return _isShow;
            }
            set
            {
                if (_isShow == value) return;
                _isShow = value;

                string iconTooltip = string.Empty; // 图标提示信息
                if (value.HasValue && value.Value) // 在大屏上显示
                {
                    panelNotifyIcon.BackgroundImage = Image.FromFile(@".\Image\BlueDot.png");
                    iconTooltip = SurveillanceUtilities.ALREADY_ON_THE_WALL;
                    timer.Start();

                    btnShowWall.Enabled = false;
                    btnCloseWall.Enabled = !btnShowWall.Enabled;
                }
                else if (value.HasValue && !value.Value)
                {
                    panelNotifyIcon.BackgroundImage = Image.FromFile(@".\Image\RedDot.png");
                    iconTooltip = SurveillanceUtilities.NOT_ON_THE_WALL;
                    timer.Stop();

                    btnShowWall.Enabled = true;
                    btnCloseWall.Enabled = !btnShowWall.Enabled;
                }
                else
                {
                    panelNotifyIcon.BackgroundImage = Image.FromFile(@".\Image\SkyDot.png");
                    iconTooltip = SurveillanceUtilities.ALREADY_ON_THE_WALL;
                    timer.Start();

                    btnShowWall.Enabled = false;
                    btnCloseWall.Enabled = !btnShowWall.Enabled;
                }

                toolTip.SetToolTip(panelNotifyIcon, iconTooltip);
            }
        }

        private bool _isShowButtons;
        /// <summary>
        /// 是否显示按钮
        /// </summary>
        public bool IsShowButtons
        {
            get
            {
                return _isShowButtons;
            }
            set
            {
                if (_isShowButtons == value) return;
                _isShowButtons = value;

                btnEditWall.Visible = btnCloseWall.Visible = btnShowWall.Visible = value;
            }
        }

        #endregion

        #region 变量

        Action<int> showEditWallDetailAction; // 显示编辑详情界面
        Action<int> showWallAction; // 上墙显示
        Action<int> closeWallAction; // 关闭上墙的大屏

        Timer timer = new Timer();
        bool isBlink = true;

        #endregion

        #region 构造函数

        /// <summary>
        /// 
        /// </summary>
        /// <param name="showEditWallDetail">显示编辑详情界面</param>
        public WallPreview(Action<int> showEditWallDetail,
            Action<int> showWall, Action<int> closeWall)
        {
            InitializeComponent();

            this.showEditWallDetailAction = showEditWallDetail;
            this.showWallAction = showWall;
            this.closeWallAction = closeWall;
            IsShowWall = false; // 大屏未上墙
            IsShowButtons = false; // 按钮不显示

            #region Timer
            timer.Interval = 1000; // 单位：毫秒
            timer.Tick += (s, e) =>
            {
                blinkImage(isBlink);
            };
            #endregion
        }

        #endregion

        #region 方法

        #region 公开的方法
        /// <summary>
        /// 显示按钮
        /// </summary>
        public void ShowButtons()
        {
            if (SurveillanceUtilities.PanelAllWall != null &&
                SurveillanceUtilities.PanelAllWall.Controls != null &&
                SurveillanceUtilities.PanelAllWall.Controls.Count > 0)
            {
                // 1. 不显示所有预览模块按钮
                for (int i = 0; i < SurveillanceUtilities.PanelAllWall.Controls.Count; i++)
                {
                    WallPreview preWall = SurveillanceUtilities.PanelAllWall.Controls[i] as WallPreview;
                    preWall.IsShowButtons = false;
                }

                IsShowButtons = true; // 2. 显示按钮
            }
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 闪烁图片
        /// </summary>
        /// <param name="isShow"></param>
        private void blinkImage(bool isShow)
        {
            if (isShow)
                IsShowWall = true;
            else
                IsShowWall = null;

            isBlink = !isShow;
        }
        #endregion

        #endregion

        #region 事件

        private void WallPreview_MouseEnter(object sender, EventArgs e)
        {
            ShowButtons(); // 显示按钮
        }

        /// <summary>
        /// 详情：显示编辑详情界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEditWall_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.showEditWallDetailAction != null && this.Tag != null)
                {
                    this.showEditWallDetailAction((this.Tag as TagModel).Wall_Index); // 显示编辑详情界面
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 显示：显示到大屏上
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShowWall_Click(object sender, EventArgs e)
        {
            try
            {
                IsShowWall = true; // 大屏显示
                timer.Start();
                if (this.Tag != null &&
                    this.Tag is TagModel && this.showWallAction != null)
                {
                    (this.Tag as TagModel).Wall_IsShow = true;


                    if (Utilities.DebugModel)
                    {
                        this.showWallAction((this.Tag as TagModel).Wall_Index); // 上墙显示
                    }
                    else
                    {
                        this.showWallAction((this.Tag as TagModel).Wall_Index + 1); // 上墙显示
                    }
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 关闭：关闭对应大屏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCloseWall_Click(object sender, EventArgs e)
        {
            try
            {
                IsShowWall = false; // 取消大屏显示
                timer.Stop();
                if (this.Tag != null &&
                    this.Tag is TagModel && this.closeWallAction != null)
                {
                    (this.Tag as TagModel).Wall_IsShow = false;


                    if (Utilities.DebugModel)
                    {
                        this.closeWallAction((this.Tag as TagModel).Wall_Index); // 关闭大屏
                    }
                    else
                    {
                        this.closeWallAction((this.Tag as TagModel).Wall_Index + 1); // 关闭大屏
                    }
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion
    }
}
